package com.info.zhiduoduo.core.service.impl;

import com.info.zhiduoduo.common.configs.UploadPathConfig;
import com.info.zhiduoduo.common.configs.ZddUrlConfig;
import com.info.zhiduoduo.common.utils.DateUtil;
import com.info.zhiduoduo.common.utils.QrcodeUtil;
import com.info.zhiduoduo.common.utils.RandomUtil;
import com.info.zhiduoduo.core.service.CoreMarketingCooperativeService;
import com.info.zhiduoduo.repository.entity.marketingcooperative.MarketingCooperativePosterConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class CoreMarketingCooperativeServiceImpl implements CoreMarketingCooperativeService {

	@Autowired
	UploadPathConfig uploadPathConfig;
	@Autowired
	ZddUrlConfig zddUrlConfig;

	@Override
	public void generatePoster(InputStream srcIs, OutputStream os, MarketingCooperativePosterConfig poster, int partnerid)
			throws Exception {

		BufferedImage srcImg = ImageIO.read(srcIs);

		//判断是否需要显示二维码
		if (poster.getQrCodeOrNot() != null && poster.getQrCodeOrNot() == 0) {
			// 生成二维码
			String url = String.format(zddUrlConfig.getMarketingCooperativeFormUrl(), poster.getId(), partnerid);
			ByteArrayOutputStream qrcodeOs = new ByteArrayOutputStream();
			QrcodeUtil.encode(url, qrcodeOs);
			InputStream qrcodeIs = new ByteArrayInputStream(qrcodeOs.toByteArray());

			//画二维码
			int qrcodeX = poster.getQrcodeX();
			int qrcodeY = poster.getQrcodeY();
			int qrcodeWidth = poster.getQrcodeW();
			int qrcodeHeight = poster.getQrcodeH();

			ImageIcon qrcodeImgIcon = new ImageIcon(ImageIO.read(qrcodeIs));
			Image qrcodeImg = qrcodeImgIcon.getImage();
			Image scaleQrcode = qrcodeImg.getScaledInstance(qrcodeWidth, -1, Image.SCALE_DEFAULT);

			Graphics2D graphics = srcImg.createGraphics();
			graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
			// //绘制圆形的头像区域
			// Ellipse2D.Double shape = new Ellipse2D.Double(qrcodeX, qrcodeY, qrcodeWidth, qrcodeHeight);
			// graphics.setClip(shape);

			graphics.setColor(Color.WHITE);
			graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 1.0f));

			//水印 的位置
			graphics.drawImage(scaleQrcode, qrcodeX, qrcodeY, null);
			graphics.dispose();
			qrcodeOs.close();
			qrcodeIs.close();
		}

		// 生成图片
		ImageIO.write(srcImg, "png", os);
	}

	@Override
	public String generatePosterImg(int partnerid, MarketingCooperativePosterConfig poster) {
		try {
			// ClassPathResource resource = new ClassPathResource("img/poster.png");
			// InputStream srcIs = resource.getInputStream();
			File posterFile = new File(uploadPathConfig.getUploadpath(), poster.getPosterPath());
			InputStream srcIs = new FileInputStream(posterFile);

			String date = DateUtil.getString(new Date(), DateUtil.FORMAT_STYLE_8);
			File pathFile = new File(uploadPathConfig.getUploadpath(), date);
			if (!pathFile.exists()) {
				pathFile.mkdirs();
			}
			String filename = RandomUtil.createRandomNo("mc_poster_", ".png");
			File outputImageFile = new File(pathFile, filename);

			try (OutputStream os = new FileOutputStream(outputImageFile)) {
				// 生成海报
				generatePoster(srcIs, os, poster, partnerid);

				return date + File.separator + filename;
			} catch (Exception e) {
				log.error("生成海报报错", e);
			}
		} catch (Exception e) {
			log.error("生成推广海报失败：", e);
		}
		return null;
	}
}
